package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.domain.Attachment;
import com.ruoyi.system.api.domain.AttachmentVO;
import com.ruoyi.system.service.IAttachmentAssociatedService;
import com.ruoyi.system.service.IAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 附件Controller
 *
 * @author luogongquan
 * @date 2024-09-18
 */
@RestController
@RequestMapping("/attachment")
public class AttachmentController extends BaseController {
    @Autowired
    private IAttachmentService attachmentService;
    @Autowired
    private IAttachmentAssociatedService attachmentAssociatedService;

    /**
     * 查询附件列表
     */
//    @RequiresPermissions("system:attachment:list")
    @GetMapping("/list")
    public TableDataInfo list(Attachment attachment) {
        startPage();
        List<Attachment> list = attachmentService.selectAttachmentList(attachment);
        return getDataTable(list);
    }

    /**
     * 导出附件列表
     */
//    @RequiresPermissions("system:attachment:export")
    @Log(title = "附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Attachment attachment) {
        List<Attachment> list = attachmentService.selectAttachmentList(attachment);
        ExcelUtil<Attachment> util = new ExcelUtil<Attachment>(Attachment.class);
        util.exportExcel(response, list, "附件数据");
    }

    /**
     * 获取附件详细信息
     */
//    @RequiresPermissions("system:attachment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(attachmentService.selectAttachmentById(id));
    }

    /**
     * 新增附件
     */
//    @RequiresPermissions("system:attachment:add")
    @Log(title = "附件", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Integer> add(@RequestBody Attachment attachment) {
        return R.ok(attachmentService.insertAttachment(attachment));
    }

    /**
     * 新增附件
     */
//    @RequiresPermissions("system:attachment:add")
    @InnerAuth
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PostMapping("batchAdd")
    public R<Integer> batchAdd(@RequestBody AttachmentVO vo) {
        return R.ok(attachmentService.batchInsertAttachment(vo));
    }

    /**
     * 新增附件
     */
//    @RequiresPermissions("system:attachment:add")
    @InnerAuth
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @PostMapping("batchDeleteAttachment")
    public R batchDeleteAttachment(@RequestBody List<Long> associatedIds) {
        attachmentService.batchDeleteAttachment(associatedIds);
        return R.ok();
    }

    /**
     * 修改附件
     */
//    @RequiresPermissions("system:attachment:edit")
    @Log(title = "附件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Attachment attachment) {
        return toAjax(attachmentService.updateAttachment(attachment));
    }

    /**
     * 删除附件
     */
//    @RequiresPermissions("system:attachment:remove")
    @Log(title = "附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(attachmentService.deleteAttachmentByIds(ids));
    }

    //    @RequiresPermissions("system:attachment:list")
    @GetMapping(value = "viewByAssociatedId/{associatedId}")
    public AjaxResult viewByAssociatedId(@PathVariable("associatedId") Long associatedId) {
        return success(attachmentService.viewByAssociatedId(associatedId));
    }

    //    @RequiresPermissions("system:attachment:list")
    @GetMapping(value = "getPictureByAssociatedId/{associatedId}")
    public AjaxResult getPictureByAssociatedId(@PathVariable("associatedId") Long associatedId) {
        return success(attachmentService.getPictureByAssociatedId(associatedId));
    }
}
